<template>
	<view class="search-container">
		<!-- 搜索头部 -->
		<view class="search-header">
			<text class="back-icon" @tap="handleClose">←</text>
			<text class="search-title">搜索</text>
		</view>
		
		<!-- 搜索栏 -->
		<view class="search-bar">
			<view class="search-input-wrapper">
				<text class="search-icon">🔍</text>
				<input 
					class="search-input" 
					type="text" 
					placeholder="搜索歌曲、歌手" 
					:value="searchKeyword"
					@input="handleSearch"
					@confirm="handleSearchConfirm"
				/>
				<text v-if="searchKeyword" class="clear-icon" @tap="clearSearch">×</text>
			</view>
		</view>
		
		<!-- 搜索结果 -->
		<view v-if="showResults" class="search-results">
			<view v-if="searchResults.length > 0" class="results-list">
				<view 
					v-for="(song, index) in searchResults" 
					:key="index" 
					class="result-item"
					@tap="playSong(song)"
				>
					<image :src="song.cover" class="result-cover" mode="aspectFill" />
					<view class="result-info">
						<text class="song-name">{{ song.name }}</text>
						<text class="song-artist">{{ song.artist }}</text>
					</view>
					<text class="play-icon">▶</text>
				</view>
			</view>
			<view v-else-if="searchKeyword" class="no-results">
				<text class="no-results-text">没有找到相关歌曲</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:"searchText",
		data() {
			return {
				searchKeyword: '',
				searchResults: [],
				showResults: false,
				searchTimer: null
			};
		},
		methods: {
			handleSearch(e) {
				this.searchKeyword = e.detail.value
				// 防抖处理
				clearTimeout(this.searchTimer)
				this.searchTimer = setTimeout(() => {
					this.performSearch()
				}, 300)
			},
			
			handleSearchConfirm() {
				this.performSearch()
			},
			
			performSearch() {
				if (!this.searchKeyword.trim()) {
					this.searchResults = []
					this.showResults = false
					return
				}
				
				// 模拟搜索数据（兼容微信小程序环境）
				const mockSongs = [
					{
						id: 1,
						name: '骄傲',
						artist: '王源',
						cover: 'https://ai-public.mastergo.com/ai/img_res/9b30f4ee0302b8d25ce0fca941c22eca.jpg',
						url: '/music/wangyuan/jiaoao.mp3',
						duration: 240
					},
					{
						id: 2,
						name: '夜的钢琴曲',
						artist: '石进',
						cover: 'https://ai-public.mastergo.com/ai/img_res/c1ce38ecf2bd6fbe61281df433ca6b50.jpg',
						url: '/music/shijin/yedegangqinqu.mp3',
						duration: 180
					},
					{
						id: 3,
						name: '童话镇',
						artist: '陈一发儿',
						cover: 'https://ai-public.mastergo.com/ai/img_res/f9fc38e361ab67a35c0e5506eddd71df.jpg',
						url: '/music/chenyifaer/tonghuazhen.mp3',
						duration: 210
					},
					{
						id: 4,
						name: '起风了',
						artist: '买辣椒也用券',
						cover: 'https://ai-public.mastergo.com/ai/img_res/45336b12831b6117c08a852940c79512.jpg',
						url: '/music/malajiaoyeyongquan/qifengle.mp3',
						duration: 195
					}
				]
				
				const searchTerm = this.searchKeyword.toLowerCase().trim()
				const results = mockSongs.filter(song => 
					song.name.toLowerCase().includes(searchTerm) || 
					song.artist.toLowerCase().includes(searchTerm)
				)
				
				this.searchResults = results
				this.showResults = true
			},
			
			clearSearch() {
				this.searchKeyword = ''
				this.searchResults = []
				this.showResults = false
			},
			
			playSong(song) {
				// 触发父组件播放歌曲
				this.$emit('play-song', song)
				this.handleClose()
			},
			
			handleClose() {
				this.$emit('close')
			}
		}
	}
</script>

<style scoped>
.search-container {
	width: 100%;
	height: 100vh;
	background-color: #fff;
	position: fixed;
	top: 0;
	left: 0;
	z-index: 1000;
}

.search-header {
	display: flex;
	align-items: center;
	padding: 20rpx 32rpx;
	background-color: #fff;
	border-bottom: 1px solid #f0f0f0;
	padding-top: calc(env(safe-area-inset-top, 40rpx) + 20rpx);
}

.back-icon {
	font-size: 36rpx;
	color: #333;
	margin-right: 24rpx;
	padding: 16rpx;
}

.search-title {
	font-size: 32rpx;
	font-weight: 500;
	color: #333;
}

.search-bar {
	padding: 20rpx 32rpx;
	background-color: #fff;
	border-bottom: 1px solid #f0f0f0;
}

.search-input-wrapper {
	position: relative;
	display: flex;
	align-items: center;
	background-color: #f8f8f8;
	border-radius: 50rpx;
	padding: 16rpx 24rpx;
}

.search-icon {
	font-size: 28rpx;
	color: #999;
	margin-right: 16rpx;
}

.search-input {
	flex: 1;
	font-size: 28rpx;
	color: #333;
	background: transparent;
	border: none;
	outline: none;
}

.clear-icon {
	font-size: 32rpx;
	color: #999;
	padding: 8rpx;
}

.search-results {
	background-color: #fff;
	padding-top: 20rpx;
}

.results-list {
	padding: 0 32rpx;
}

.result-item {
	display: flex;
	align-items: center;
	padding: 24rpx 0;
	border-bottom: 1px solid #f0f0f0;
}

.result-item:last-child {
	border-bottom: none;
}

.result-cover {
	width: 80rpx;
	height: 80rpx;
	border-radius: 12rpx;
	margin-right: 24rpx;
}

.result-info {
	flex: 1;
	min-width: 0;
}

.song-name {
	font-size: 28rpx;
	font-weight: 500;
	color: #333;
	display: block;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.song-artist {
	font-size: 24rpx;
	color: #999;
	margin-top: 8rpx;
	display: block;
}

.play-icon {
	font-size: 32rpx;
	color: #007aff;
	padding: 16rpx;
}

.no-results {
	padding: 80rpx 32rpx;
	text-align: center;
}

.no-results-text {
	font-size: 28rpx;
	color: #999;
}
</style>